package com.itheima;

import com.ithema.mapper.UserMapper;
import com.ithema.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;

public class Demo {


    @Test
    public void select() throws IOException {
        // 编写代码让MyBatis跑起来,执行SQL语句
        String resource = "mybatis-config.xml";
        // 加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 得到SqlSession工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 得到SqlSession(相当于数据库连接,操作数据库的对象)
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 生成UserMapper接口的实现类对象(动态代理)
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = mapper.selectById(4);
        System.out.println(user);

    }

    @Test
    public void Test01() throws IOException {
        // 编写代码让MyBatis跑起来,执行SQL语句
        String resource = "mybatis-config.xml";
        // 加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 得到SqlSession工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 得到SqlSession(相当于数据库连接,操作数据库的对象)
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        // 生成UserMapper接口的实现类对象(动态代理)
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteById(3);
        sqlSession.close();

    }

    @Test
    public void test04() throws IOException {
        // 加载核心配置文件，获取SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获取SqlSession对象，执行SQL语句
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        // 参数
        // User user = new User(1, "孙悟空", Date.valueOf("1999-09-09"), "妖", "花果山");
        // 动态SQL
        User user = new User(1, "孙悟空",Date.valueOf("1999-09-09"), "男", "五指山");
        mapper.update(user);
        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }

    @Test
    public void test05() throws IOException {
        // 加载核心配置文件，获取SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获取SqlSession对象，执行SQL语句
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int[] ids = new int[]{1, 2};
        mapper.deleteByIds(ids);
        sqlSession.close();

    }
}
